package com.zero.zerocat;

import com.zero.servlet.ZeroRequest;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.QueryStringDecoder;

import java.util.List;
import java.util.Map;

/**
 * ZeroCat中对ZeroRequest规范的实现类
 */
public class HttpZeroRequest implements ZeroRequest {
    // 引入HttpRequest对象作为请求的载体
    private HttpRequest request;

    public HttpZeroRequest(HttpRequest request){
        this.request = request;
    }

    @Override
    public String getUri() {
        return request.uri();
    }

    @Override
    public String getPath() {
        QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
        return decoder.path();
    }

    @Override
    public String getMethod() {
        return request.method().name();
    }

    @Override
    public Map<String, List<String>> getParameters() {
        QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
        return decoder.parameters();
    }

    @Override
    public List<String> getParameters(String name) {
        return getParameters().get(name);
    }

    @Override
    public String getParameter(String name) {
        List<String> parameterList = getParameters().get(name);
        if (parameterList == null || parameterList.size() == 0){
            return null;
        }
        return parameterList.get(0);
    }
}
